<template>
  <div id="column-edit-dialog">
    <el-dialog :visible.sync="dialogVisible" width="80%" @closed="clearDialog">
      <span slot="title">
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item class="font-weight">专栏</el-breadcrumb-item>
          <el-breadcrumb-item>专栏编辑</el-breadcrumb-item>
        </el-breadcrumb>
      </span>
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="150px">
        <el-form-item label="分类专栏名称:" prop="name">
          <el-col :span="15">
            <el-input v-model="ruleForm.name" placeholder="请输入分类专栏名称"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="分类专栏简介:" prop="description">
          <el-col :span="15">
            <el-input
              type="textarea"
              v-model="ruleForm.description"
              placeholder="请输入专栏简介,最大不能超过200个字符"
              :rows="5"
              resize="none"
              maxlength="200"
              show-word-limit
            ></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="分类专栏配图:" prop="img">
          <el-col :span="15">
            <el-upload
              class="avatar-uploader"
              :action="uploadUrl"
              :show-file-list="false"
              name="file[]"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload"
            >
              <img v-if="ruleForm.img" :src="ruleForm.img" class="avatar" />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-col>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      dialogVisible: false,
      dialogType: 'edit',
      uploadUrl: window.INIT_CONFIG['baseUrl'] + '/upload',
      ruleForm: {
        name: '',
        description: '',
        img: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入专栏名称', trigger: 'blur' },
          { min: 1, max: 10, message: '长度在 1 到 10 个字符', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '请填写专栏简介', trigger: 'blur' },
          { max: 200, message: '专栏简介不能超过200个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    handleAvatarSuccess(res, file) {
      URL.createObjectURL(file.raw)
      this.ruleForm.img = file.response.data.succMap.url
    },
    beforeAvatarUpload(file) {
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isLt2M
    },
    showDialog(params) {
      this.dialogType = params.type
      if (params.type === 'edit') {
        this.ruleForm.id = params.data.id
        this.ruleForm.name = params.data.name
        this.ruleForm.description = params.data.description
        this.ruleForm.img = params.data.img
      }

      this.dialogVisible = true
    },
    submitForm(formName) {
      let url = ''
      if (this.dialogType === 'add') {
        url = `/column`
      } else {
        url = `/column/${this.ruleForm.id}`
      }
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$ajax({
            url: this.$ajax.setUrl(url),
            method: this.dialogType === 'add' ? 'post' : 'put',
            data: this.$ajax.setData(this.ruleForm)
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$emit('refresh')
              this.$message.success('专栏更新成功')
            } else {
              this.$message.error(data.data || data.msg)
            }
          })
          this.dialogVisible = false
        } else {
          alert('error submit!!')
          return false
        }
      })
    },
    clearDialog() {
      // 初始化数据
      Object.assign(this.$data, this.$options.data.call(this))
      // 清空验证
      this.$nextTick(() => {
        this.$refs.ruleForm.clearValidate()
      })
    }
  }
}
</script>

<style lang="scss">
#column-edit-dialog {
  .font-weight {
    font-weight: bold;
  }
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409eff;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
}
</style>
